﻿#include "loopthreadpool.h"
#include "eventloop.h"
#include "loopthread.h"

LoopThreadPool::LoopThreadPool(EventLoop* baseloop)
    : m_baseLoop(baseloop), m_threadCount(0), m_nextLoopStep(0)
{
}

LoopThreadPool::~LoopThreadPool()
{
}

void LoopThreadPool::setThreadCount(int threadCount)
{
    m_threadCount = threadCount;
}

void LoopThreadPool::create()
{
    if (m_threadCount > 0)
    {
        m_threads.resize(m_threadCount);
        m_loops.resize(m_threadCount);
        for (int i = 0; i < m_threadCount; i++)
        {
            m_threads[i] = new LoopThread();
            m_loops[i] = m_threads[i]->getLoop();
        }
    }
    return;
}

EventLoop* LoopThreadPool::nextLoop()
{
    if (m_threadCount == 0)
        return m_baseLoop;
    m_nextLoopStep = (m_nextLoopStep + 1) % m_threadCount;
    return m_loops[m_nextLoopStep];
}

